<?php

namespace app\data\controller\api;

use app\data\service\NewsService;
use think\admin\Controller;

/**
 * 文章接口控制器
 * Class News
 * @package app\data\controller\api
 */
class News extends Controller
{
    /**
     * 获取文章分类列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCate()
    {
        $query = $this->_query('DataNewsCate')->like('name');
        $query->where(['status' => 1, 'deleted' => 0])->withoutField('sort,status,deleted');
        $this->success('获取文章分类', $query->order('sort desc,id desc')->page(false, false));
    }

    /**
     * 获取文章标签列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMark()
    {
        $query = $this->_query('DataNewsMark')->like('name');
        $query->where(['status' => 1, 'deleted' => 0])->withoutField('sort,status,deleted');
        $this->success('获取文章标签', $query->order('sort desc,id desc')->page(false, false));
    }

    /**
     * 获取文章内容列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getItem()
    {
        if ($code = input('code', '')) {
            $this->app->db->name('DataNewsItem')->where(['code' => $code])->inc('num_read')->update();
            if (($uuid = input('uuid', 0)) > 0) {
                $data = ['uuid' => $uuid, 'code' => $code, 'type' => 3, 'status' => 2];
                $this->app->db->name('DataNewsXCollect')->where($data)->delete();
                $this->app->db->name('DataNewsXCollect')->insert($data);
            }
        }
        $query = $this->_query('DataNewsItem')->like('name,mark')->equal('id,code,cate_id');
        $query->where(['deleted' => 0, 'status' => 1])->withoutField('sort,status,deleted');
        $result = $query->order('sort desc,id desc')->page(true, false, false, input('limit', 15));
        NewsService::instance()->buildData($result['list'], input('uuid', 0));
        $this->success('获取文章内容', $result);
    }

    /**
     * 获取文章内容详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getItemDetail()
    {
        $data = $this->_vali([
            'code.require' => '文章不能为空！'
        ]);
        if ($code = $data['code']) {
            $this->app->db->name('DataNewsItem')->where(['code' => $code])->inc('num_read')->update();
        }
        $query = $this->app->db->table('data_news_item')->where('code', $code);
        $query->where(['deleted' => 0, 'status' => 1])->withoutField('status,deleted');
        $result = $query->find();
        if (!$result) $this->error('查无该文章');

        $marks = $this->app->db->name('DataNewsMark')->where(['status' => 1])->column('name');
        $result['mark'] = str2arr($result['mark'] ?: '', ',', $marks);

        $result['the_previous'] = $this->app->db->name('DataNewsItem')
            ->where(['deleted' => 0, 'status' => 1])
            ->where('sort', '>=', $result['sort'])
            ->where('id', '>', $result['id'])
            ->withoutField('cate_id,mark,cover,remark,content,num_like,num_read,num_collect,num_comment,create_at')
            ->order('sort desc,id desc')
            ->find() ?: null;
        $result['the_next'] = $this->app->db->name('DataNewsItem')
            ->where(['deleted' => 0, 'status' => 1])
            ->where('sort', '<=', $result['sort'])
            ->where('id', '<', $result['id'])
            ->withoutField('cate_id,mark,cover,remark,content,num_like,num_read,num_collect,num_comment,create_at')
            ->order('sort desc,id desc')
            ->find() ?: null;

        $result['create_at'] = format_time($result['create_at']);
        $result['num_read'] = $result['num_read'] + $result['mock_read'];

        $this->success('获取文章内容', $result);
    }

    /**
     * 获取文章评论
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getComment()
    {
        $map = $this->_vali(['code.require' => '文章不能为空！']);
        $query = $this->_query('DataNewsXCollect')->where(['type' => 4, 'status' => 2]);
        $result = $query->where($map)->order('id desc')->page(true, false, false, 15);
        NewsService::instance()->buildListByUidAndCode($result['list']);
        $this->success('获取评论成功', $result);
    }

    /**
     * 获取页面配置列表
     * market_analysis=市场分析;investment_value=投资价值;operation_process=操作流程;contact_us=联系我们;large_payment=大额支付;signing_process=签约流程
     */
    public function getConfig()
    {
        $homeConfigs = [
            'market_analysis',
            'investment_value',
            'operation_process',
            'contact_us',
        ];
        $query = $this->app->db->table('data_news_config')->whereIn('code', $homeConfigs);
        $result = $query->where(['deleted' => 0])->withoutField('deleted,create_at,update_at,num_read,config_key')->select();
        foreach ($result as &$value) {
            formatContent($value['content']);
        }
        $this->success('获取首页配置成功', $result);
    }

    /**
     * 获取页面配置详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getConfigDetail()
    {
        $code = input('code');
        if ($code == 'signing_process') $code = 'operation_process';
        $query = $this->app->db->table('data_news_config')->where('code', $code);
        $result = $query->where(['deleted' => 0])->withoutField('deleted,create_at,update_at,num_read,config_key')->find();
        if (!empty($result['content'])) {
            $result['content'] = formatContent($result['content']);
        }
        $this->success('获取页面配置详情成功', $result ?: []);
    }

}